package leetcode_day._2021._202105._1120;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/5/16 21:31
 * 罗马数字转整数
 * 算法：字符串
 */
public class _15_13 {

    public int romanToInt(String s) {
        String[] x = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        Integer[] y = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        int ans = 0, cur = 0;
        for (int i = 0; i < x.length; i++) {
            while (cur + x[i].length() <= s.length() && s.substring(cur, cur + x[i].length()).equals(x[i])) {
                ans += y[i];
                cur += x[i].length();
            }
        }
        return ans;
    }
}
